﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using WINBASE.Utils.DBHelper;
using WINBASE.Controls;
using WINBASE.Utils;

namespace WINBASE
{
    public partial class frmFmam : WLCForm
    {
        DataRow drSelectedMenu;
        DataRow drSelectedNode;
        DataView dvNodes;

        public frmFmam()
        {
            InitializeComponent();
            InitializeUI();
            InitializeData();
        }

        public void InitializeUI()
        {
            InitModule();
            InitDataGrid();
        }

        public Object DataSource
        {
            get { return this.DataGrid.DataSource; }
            set
            {
                this.DataGrid.DataSource = value;    
            }
        }

        public void InitDataGrid()
        {
            this.DataGrid = new WLCDataGridView();

            ((System.ComponentModel.ISupportInitialize)(this.DataGrid)).BeginInit();
            this.SuspendLayout();

            WLCPnlGrid.Controls.Add(this.DataGrid);
            
            ((System.ComponentModel.ISupportInitialize)(this.DataGrid)).EndInit();
            this.ResumeLayout(false);
        }
        
        public void InitModule()
        {
            foreach (DataRow drMenu in ApplicationData.DtModule.Rows)
            {
                int ModuleTrxNo = (int)drMenu["Trx No"];
                string ModuleCode = (string)drMenu["Module Code"];
                string ModuleName = (string)drMenu["Module Name"];

                TreeNode myModNode = new WLCTreeNode(ModuleTrxNo,"trn" + ModuleCode,ModuleName);
                myModNode.Tag = ModuleCode;

                var drNodes = ApplicationData.dtNodes.AsEnumerable().Where(x => x.Field<int>("Module Trx No") == (int)ModuleTrxNo && 
                                                                                x.Field<object>("Parent Trx No") == null);
                foreach (DataRow drNode in drNodes)
                {
                    int ContentTrxNo = (int)drNode["Trx No"];
                    string ContentName = (string)drNode["Node Name"];
                    string ContentDesc = (string)drNode["Node Description"];
                    WLCTreeNode myContentNode = new WLCTreeNode(ContentTrxNo,"trn" + ContentName,ContentDesc);
                    myContentNode = addChildNodes(myContentNode);
                    myModNode.Nodes.Add(myContentNode);
                }
                trvModuleList.Nodes.Add(myModNode);
                
                TabPage mytab = new TabPage(ModuleName);
                mytab.Name = "tab" + ModuleCode;
                mytab.Tag = ModuleCode;
                tabMenu.TabPages.Add(mytab);
            }
        }

        private WLCTreeNode addChildNodes(WLCTreeNode ParentNode)
        {
            var drChildRows = ApplicationData.DtNodes.AsEnumerable()
                            .Where(x =>
                                x["Parent Trx No"] != DBNull.Value && 
                                (int)x["Parent Trx No"] == ParentNode.ControlId).ToList();
            int i = 0;
            foreach (DataRow drNode in drChildRows)
            {
                WLCTreeNode ChildNode = new WLCTreeNode((int)drNode["Trx No"], "trn" + (string)drNode["Node Name"], (string)drNode["Node Description"]);
                ParentNode.Nodes.Add(ChildNode);

                var drSubChildRow = ApplicationData.DtNodes.Rows
                            .Cast<DataRow>()
                            .Where(x =>
                                x["Parent Trx No"] != DBNull.Value &&
                                (int)x["Parent Trx No"] == (int)drNode["Trx No"]).ToList();
                if (drSubChildRow.Count > 0) addChildNodes(ChildNode);
                i++;
            }
            return ParentNode;
        }

        public void InitializeData()
        {
            dvNodes = ApplicationData.DtNodes.DefaultView;
            dvNodes.Sort = "Sort ASC";
        }

        private void tabMenu_SelectedIndexChanged(object sender, EventArgs e)
        {
            drSelectedMenu = ApplicationData.DtModule.AsEnumerable()
                            .Where(x => (string)x["Module Code"] == (string)tabMenu.TabPages[tabMenu.SelectedIndex].Tag).Single();

            int ModuleTrxNo = (int)drSelectedMenu["Trx No"];

            dvNodes.RowFilter = string.Format("[Module Trx No] = {0}", ModuleTrxNo);
            this.DataGrid.DataSource = dvNodes;
        }

        private void trvModuleList_AfterSelect(object sender, TreeViewEventArgs e)
        {
            //Select Module Tab
            if (e.Node.Parent == null)
            {
                int selectedIndex = tabMenu.TabPages.IndexOfKey("tab" + e.Node.Tag);
                if (tabMenu.SelectedIndex == selectedIndex)
                {
                    tabMenu_SelectedIndexChanged(null, null);
                }
                else
                {
                    tabMenu.SelectedIndex = selectedIndex;
                }
            }
            else
            {
                int ContentTrxNo = ((WLCTreeNode)e.Node).ControlId;
                drSelectedNode = ApplicationData.dtNodes.AsEnumerable()
                            .Where(x => (int)x["Trx No"] == ContentTrxNo).Single();
                int ModuleTrxNo = (int)drSelectedNode["Module Trx No"];
                drSelectedMenu = ApplicationData.DtModule.AsEnumerable()
                                .Where(x => (int)x["Trx No"] == ModuleTrxNo).Single();
                string ModuleCode = (string)drSelectedMenu["Module Code"];

                int selectedIndex = tabMenu.TabPages.IndexOfKey("tab" + ModuleCode);
                if (tabMenu.SelectedIndex == selectedIndex)
                {
                    tabMenu_SelectedIndexChanged(null, null);
                }
                else
                {
                    tabMenu.SelectedIndex = selectedIndex;
                }

                //Select Content Detail
                DataGridViewRow currCtRow = this.DataGrid.Rows
                                            .Cast<DataGridViewRow>()
                                            .Where(r => r.Cells["colTrxNo"].Value.Equals(ContentTrxNo))
                                            .First();

                currCtRow.Selected = true;
            }
        }

        private void tspMain_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            ContainsSwitch switchAction = new ContainsSwitch();
            switchAction.AddCase(mnuAddModule.Name, delegate() 
            {
                MessageBox.Show("har har");
            });
            
            switchAction.Perform(e.ClickedItem.Name);
        }

        private void mnuAddModule_Click(object sender, EventArgs e)
        {
            
        }

        public WLCDataGridView DataGrid
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public WLCViewToolStrip DataGridViewFilter
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
    }
}